package com.songlanyun.modules.marketing.firstorder.service.impl;

import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.songlanyun.common.enums.MarketingConstant;
import com.songlanyun.common.exception.RRException;
import com.songlanyun.common.utils.BeanUtil;
import com.songlanyun.modules.exception.MarketingException;
import com.songlanyun.modules.marketing.firstorder.dao.FirstOrderActivityDao;
import com.songlanyun.modules.marketing.firstorder.entity.FirstOrderActivity;
import com.songlanyun.modules.marketing.firstorder.model.dto.FirstOrderActivityDTO;
import com.songlanyun.modules.marketing.firstorder.service.FirstOrderActivityService;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.songlanyun.common.utils.PageUtils;
import com.songlanyun.common.utils.Query;


@Service("yxtMarketingFirstOrderActivityService")
public class FirstOrderActivityServiceImpl extends ServiceImpl<FirstOrderActivityDao, FirstOrderActivity> implements FirstOrderActivityService {

    @Override
    public PageUtils queryPage(Map<String, Object> params, Long shopId) {
        Object isUse = params.get("is_use");
        Boolean use = null;
        if (isUse != null) {
            use = MapUtil.getBool(params, "is_use");
        }

        IPage<FirstOrderActivity> page = this.page(
                new Query<FirstOrderActivity>().getPage(params),
                new LambdaQueryWrapper<FirstOrderActivity>()
                        .eq(FirstOrderActivity::getShopId, shopId)
                        .eq(use != null, FirstOrderActivity::getIsUse, use)
                        .orderByDesc(FirstOrderActivity::getCreateTime)
        );

        return new PageUtils(page);
    }

    @Override
    public void saveOrder(FirstOrderActivityDTO dto) {

        //查询商家的首单优惠
        FirstOrderActivity firstOrderActivity = this.getByShopId(dto.getShopId(), false);
        if (firstOrderActivity == null) {
            firstOrderActivity = new FirstOrderActivity();
        }

        BeanUtil.copyPropertiesIgnoreNull(dto, firstOrderActivity);//拷贝
        if(firstOrderActivity.getIsUse() && (ObjectUtil.isNull(firstOrderActivity.getFull()) || ObjectUtil.isNull(firstOrderActivity.getReduction())))
        {
            throw new RRException("满、减多少元不能为空");
        }
        if(firstOrderActivity.getIsUse() && firstOrderActivity.getFull().compareTo(firstOrderActivity.getReduction())<0)
        {
            throw new RRException("门槛值必须大于满减值");
        }
        if(!firstOrderActivity.getIsUse())
        {
            firstOrderActivity.setFull(BigDecimal.ZERO);
            firstOrderActivity.setReduction(BigDecimal.ZERO);
        }

        this.saveOrUpdate(firstOrderActivity);

    }

    @Override
    public FirstOrderActivity shopList(Long shopId) {
        return this.baseMapper.selectOne(
                new LambdaQueryWrapper<FirstOrderActivity>()
                        .eq(FirstOrderActivity::getShopId, shopId)
                        .eq(FirstOrderActivity::getIsUse, true)//启用
                        .orderByDesc(FirstOrderActivity::getCreateTime)
                        .last("limit 1")
        );
    }

    @Override
    public FirstOrderActivity getByShopId(Long shopId, boolean isThrow) {
        FirstOrderActivity firstOrderActivity = this.baseMapper.selectOne(
                new LambdaQueryWrapper<FirstOrderActivity>()
                        .eq(FirstOrderActivity::getShopId, shopId)
                        .last("limit 1")
        );
        activityIsNull(firstOrderActivity, isThrow);

        return firstOrderActivity;
    }

    @Override
    public FirstOrderActivity getById(Long id, boolean isThrow) {
        FirstOrderActivity firstOrderActivity = this.baseMapper.selectById(id);
        activityIsNull(firstOrderActivity, isThrow);
        return firstOrderActivity;
    }

    @Override
    public FirstOrderActivity getApiByShopId(Long shopId, boolean isThrow) {
        FirstOrderActivity firstOrderActivity = this.baseMapper.selectOne(
                new LambdaQueryWrapper<FirstOrderActivity>()
                        .eq(FirstOrderActivity::getShopId, shopId)
                        .eq(FirstOrderActivity::getIsUse, true)//启用
                        .last("limit 1")
        );
        activityIsNull(firstOrderActivity, isThrow);

        return firstOrderActivity;
    }


    private void activityIsNull(FirstOrderActivity firstOrderActivity, boolean isThrow) {
        if (firstOrderActivity == null) {
            if (isThrow) {
                throw new RRException(MarketingException.FIRST_ORDER_NOT_EXIST);
            }
        }
    }

}
